indonesia_budget_data <- read.csv("./_3_data/indonesia_budget_data.csv")

doc_mod_indo_dau <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = prop_doctors, ... = dau_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAU Controls") %>% mutate(outcome = "docto")
teacher_mod_indo_dau <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = teachers_prop, ... = dau_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAU Controls") %>% mutate(outcome = "teacher")
comm_mod_indo_dau <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = prop_comm_health, ... = dau_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAU Controls") %>% mutate(outcome = "health")
schools_mod_indo_dau <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = schools_prop, ... = dau_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAU Controls") %>% mutate(outcome = "schools")
elec_mod_indo_dau <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = prop_govt_electricity, ... = dau_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAU Controls") %>% mutate(outcome = "elect")
water_mod_indo_dau <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = prop_water, ... = dau_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAU Controls") %>% mutate(outcome = "water")

doc_mod_indo_dak <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = prop_doctors, ... = dak_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAK Controls") %>% mutate(outcome = "docto")
teacher_mod_indo_dak <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = teachers_prop, ... = dak_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAK Controls") %>% mutate(outcome = "teacher")
comm_mod_indo_dak <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = prop_comm_health, ... = dak_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAK Controls") %>% mutate(outcome = "health")
schools_mod_indo_dak <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = schools_prop, ... = dak_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAK Controls") %>% mutate(outcome = "schools")
elec_mod_indo_dak <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = prop_govt_electricity, ... = dak_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAK Controls") %>% mutate(outcome = "elect")
water_mod_indo_dak <- make_scale_coefs(.df = indonesia_budget_data, .dep_var = prop_water, ... = dak_pc) %>%
  filter(term == "scaled_iv_pop") %>% mutate(country = "DAK Controls") %>% mutate(outcome = "water")




budget_controls <-
  bind_rows(comm_mod_indo_dau, schools_mod_indo_dau, doc_mod_indo_dau, teacher_mod_indo_dau, elec_mod_indo_dau, water_mod_indo_dau,
            comm_mod_indo_dak, schools_mod_indo_dak, doc_mod_indo_dak, teacher_mod_indo_dak, elec_mod_indo_dak, water_mod_indo_dak) %>%
  mutate(label = case_when(str_detect(outcome, "schools") ~ "Schools (per 1000)",
                           str_detect(outcome, "teacher") ~ "Teachers (per 1000)",
                           str_detect(outcome, "water") ~ "Piped Water (%)",
                           str_detect(outcome, "elect") ~ "Electricity Access (%)",
                           str_detect(outcome, "docto") ~ "Doctors (per 1000)",
                           str_detect(outcome, "health") ~ "Health Centers (per 1000)",
                           TRUE ~ NA_character_)) %>%
  mutate(rank = case_when(str_detect(outcome, "schools") ~ "2",
                          str_detect(outcome, "water") ~ "9",
                          str_detect(outcome, "elect") ~ "8",
                          str_detect(outcome, "teacher") ~ "6",
                          str_detect(outcome, "docto") ~ "5",
                          str_detect(outcome, "health") ~ "3",
                          TRUE ~ NA_character_)) %>%
  dplyr::select(estimate, std.error, label, country, rank) %>%
  add_row(estimate = NA_real_, std.error = NA_real_, label = "Divisible Public Goods:                             ", country = "DAU Controls", rank = "1") %>%
  add_row(estimate = NA_real_, std.error = NA_real_, label = "Personnel:                                          ", country = "DAU Controls", rank = "4") %>%
  add_row(estimate = NA_real_, std.error = NA_real_, label = "Indivisible Public Goods:                           ", country = "DAU Controls", rank = "7") %>%
  mutate(rank = as.numeric(rank)) %>%
  ggplot(aes(x=estimate, y = reorder(label, -rank))) +
  geom_point() +
  geom_errorbarh(aes(xmin=estimate-std.error*1.96, xmax = estimate+std.error*1.96), height = 0.1) +
  geom_vline(xintercept = 0, linetype = "dotted") +
  facet_wrap(country~.) +
  theme_bw() +
  scale_colour_grey() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        axis.ticks.y = element_blank(),
        legend.position = "bottom",
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        axis.title.y = element_blank()) +
  xlab("Standardized Coefficient")


ggsave("./_4_outputs/figure_oa_ii.pdf", plot = budget_controls, width = 8, height = 4)
